<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Child;
use App\Models\Grade;
use App\Models\History;
use Illuminate\Http\Request;

class AnalysisController extends BaseController
{

    public function issuccess(){
        $categories = Category::all();
        $grades = Grade::all();

        return backendView('analysis.issuccess');
    }
    /**
     * 按照平台和学校区分不同，展示不同范围的数据，
     * 显示形式相同。
     *
     * 平台展示所有
     * 学校展示本校学生
     *
     */
    public function getsuccessdata(){
        $admin = $this->getAdmin();

        if (in_array(2, $admin->roleIds())){
            //学校
            $list = History::whereHas('child',function ($query) use ($admin){
                $query->where('agency_id', $admin->agency->id);
            })->selectRaw('result, grade_id, count(id) as num')->with('grade')
                ->groupBy('result','grade_id')
                ->orderBy('grade_id')
                ->get();
        }else{
            //平台
            $list = History::selectRaw('result, grade_id, count(id) as num')->with('grade')
                ->groupBy('result','grade_id')
                ->orderBy('grade_id')
                ->get();
        }

        $result = $tmp = [];
        foreach ($list as $v){
            $tmp[$v->result][$v->grade_id] = $v->num;
        }
        for ($i=0; $i<2; $i++){
            for($j=1; $j<4; $j++){
                isset($tmp[$i][$j]) || $tmp[$i][$j] = 0;
                $result[$i][]= $tmp[$i][$j];
            }
        }

        return response()->json(['status'=>200, 'data'=>$result]);
    }

    public function basecount(){
        return backendView('analysis.trainingcount',['type'=>1]);
    }

    public function middlecount(){
        return backendView('analysis.trainingcount',['type'=>2]);
    }

    public function highcount(){
        return backendView('analysis.trainingcount',['type'=>3]);
    }

    public function gettrainingdata($type=1){
        if (!in_array($type, [1,2,3])){
            $type = 1;
        }

        $admin = $this->getAdmin();

        if (in_array(2, $admin->roleIds())){
            //学校
            $list = History::whereHas('child',function ($query) use ($admin){
                $query->where('agency_id', $admin->agency->id);
            })->selectRaw('result, grade_id, game_id, count(id) as num')
                ->with('game')
                ->groupBy('result','grade_id', 'game_id')
                ->having('grade_id', $type)
                ->orderBy('num', 'desc')
                ->get();

        }else{
            //平台
            $list = History::where('result', 1)
                ->selectRaw('result, grade_id, game_id, count(id) as num')
                ->with('game')
                ->groupBy('result','grade_id', 'game_id')
                ->having('grade_id', $type)
                ->orderBy('num', 'desc')
                ->get();
        }

        $result = ['x'=>[], 'y'=>[]];

        foreach ($list as $v){
            $result['x'][] = $v->game->name;
            $result['y'][] = $v->num;
        }

        return response()->json(['status'=>200, 'data'=>$result]);
    }

    /**
     *管理学生数量，根据角色判断，如果是学校管理员，展示每个老师管理学生数量
     * 如果是平台管理员，展示每个学校学生数量。
     *
     * @Date 2020/7/28 上午9:25
     * @return \Illuminate\Http\JsonResponse
     */

    public function children(){
        $admin = $this->getAdmin();
        $xlabelname = "机构";
        if (in_array(2, $admin->roleIds())){
            $xlabelname = '老师';
        }
        return backendView('analysis.children', compact('xlabelname'));
    }
    public function getChildrenData(){
        $admin = $this->getAdmin();
        $result = ['x'=>[], 'y'=>[]];

        if (in_array(2, $admin->roleIds())){
            //学校
            $list = Child::where('agency_id', $admin->agency->id)
                ->selectRaw('user_id, count(id) as num')
                ->with("user")
                ->groupBy('user_id')
                ->get();
            foreach ($list as $v){
                $result['x'][] = $v->user->name;
                $result['y'][] = $v->num;
            }
        }else{
            //平台
            $list = Child::selectRaw('agency_id, count(id) as num')
                ->with("agency")
                ->groupBy('agency_id')
                ->get();
            foreach ($list as $v){
                $result['x'][] = $v->agency->name;
                $result['y'][] = $v->num;
            }
        }


        return response()->json(['status'=>200, 'data'=>$result]);
    }

}
